package com.sgb.knowledgeshare.weight;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.aliyun.player.AliPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.IPlayer;
import com.aliyun.player.bean.ErrorInfo;
import com.aliyun.player.source.UrlSource;
import com.sgb.knowledgeshare.R;

/**
 * Created by：冯齐 on 2020/12/24 10:41
 * Description： 单个视频播放器
 */
public class VideoPalyerView  extends FrameLayout {
    private final Context context;
    private View mPlayerViewContainer;
    private TextureView mTextureView;
    private AliPlayer aliPlayer;

    public VideoPalyerView(@NonNull Context context) {
        this(context, null);
    }

    public VideoPalyerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        mPlayerViewContainer = View.inflate(getContext(), R.layout.video_player_view, null);
        mTextureView = mPlayerViewContainer.findViewById(R.id.video_textureview);
        aliPlayer = AliPlayerFactory.createAliPlayer(context);
        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Surface mSurface = new Surface(surface);
                if (aliPlayer != null) {
                    aliPlayer.setSurface(mSurface);
                    aliPlayer.redraw();
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                if (aliPlayer != null) {
                    aliPlayer.redraw();
                }
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                if(aliPlayer != null){
                    aliPlayer.setSurface(null);
                }
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
        //出错事件
        aliPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
            @Override
            public void onError(ErrorInfo errorInfo) {
            }
        });
        //准备成功事件
        aliPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
            @Override
            public void onPrepared() {
                 callback.onPrepared();
            }
        });
    }
    private VideoPalyerCallback callback;
    public void setCallback(VideoPalyerCallback callback) {
        this.callback = callback;
    }

    public interface VideoPalyerCallback {
        //视频准备成功的事件
        void onPrepared();
    }
    public void videoPlayUrl(String url,boolean loop) {
        //设置播放源
        UrlSource urlSource = new UrlSource();
        urlSource.setUri(url);
        if (aliPlayer!=null){
            //是否循环播放
            //准备播放
        }else {
            aliPlayer = AliPlayerFactory.createAliPlayer(context);
            //是否循环播放
            //准备播放
        }
        aliPlayer.setDataSource(urlSource);
        aliPlayer.setLoop(loop);
        aliPlayer.prepare();
    }
    // 开始播放。
    public  void  start(){
        aliPlayer.start();
    }
    // 暂停播放。
    public void pause(){
        aliPlayer.pause();
    }
    // 指定位置播放
    public void seekTo(long position){
        aliPlayer.seekTo(position);
    }
    //释放资源
    public void onDestroy() {
        aliPlayer.release();
    }

}
